package com.ddf.publics.service.impl;

import com.ddf.publics.dao.BidDao;
import com.ddf.publics.entity.Bid;
import com.ddf.publics.entity.Fund;
import com.ddf.publics.entity.UserInfo;
import com.ddf.publics.service.BidService;
import com.ddf.publics.service.FundService;
import com.ddf.publics.service.UserInfoService;
import com.ddf.publics.tool.Constant;
import com.ddf.publics.tool.MoneyFund;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 投标(Bid)表服务实现类
 *
 * @author norman
 * @since 2020-06-11 19:50:43
 */
@Service
public class BidServiceImpl implements BidService {
    @Resource
    private BidDao bidDao;

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private FundService fundService;
    /**
     * 通过ID查询单条数据
     *
     * @param bidId 主键
     * @return 实例对象
     */
    @Override
    public Bid queryById(Integer bidId) {
        return this.bidDao.queryById(bidId);
    }

    /**
     * 查询每个标 的人数
     * @return
     */
    @Override
    public List<Map> bidPieChart() {
        return this.bidDao.bidPieChart();
    }

    @Override
    public List<Map> bidList(int sid) {
        return bidDao.bidList(sid);
    }

    @Override
    public List<Map> bidList(int sid, int offset,int limit) {
        return bidDao.bidList(sid, offset, limit);
    }

    @Override
    public List<Map> bidListUid(int uid, int offset, int limit) {
        return bidDao.bidListUid(uid,offset,limit);
    }

    @Override
    public List<Map> bidListUid2(int uid, int offset, int limit) {
        return bidDao.bidListUid2(uid,offset,limit);
    }

    @Override
    public List<Map> bidListUid3(int uid, int offset, int limit) {
        List<Map> result = this.bidDao.bidListUid3(uid,offset,limit);
        return bidDao.bidListUid3(uid,offset,limit);
    }

    @Scheduled(cron = "0 0/1 * * * ? ")
    public void test(){
        List<Bid> list = bidDao.queryAllListtype(Constant.SIGN_TYPE);
        list.forEach((bid)->{
            bid.setEstimateMoney((bid.getEstimateMoney()+bid.getHqmoney_day()));
            bidDao.update(bid);
            Fund fund = new Fund();
            fund.setUId(bid.getInvestor());
            double d = bid.getEstimateMoney();
            float f = (float)d;
            fund.setFundAmunt(f);
            fund.setFundPost(MoneyFund.FUND_PAYMENT);
            fundService.insert(fund);
        });
    }

    /**
     * 查询所有数据
     *
     * @return 对象列表
     */
    @Override
    public List<Bid> queryAllList() {
        return this.bidDao.queryAllList();
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Map> queryAllByLimit(int offset, int limit) {
        return bidDao.queryAllByLimit(offset,limit);
    }

    /**
     *
     * @return
     */
    @Override
    public int totalRows() {
        return bidDao.totalRows();
    }

    @Override
    public int qtTotalRows(int sid) {
        return bidDao.qtTotalRows(sid);
    }

    @Override
    public int totalRowsId(int uid) {
        return bidDao.totalRowsId(uid);
    }

    @Override
    public int totalRowsId2(int uid) {
        return bidDao.totalRowsId2(uid);
    }

    @Override
    public int totalRowsId3(int uid) {
        return bidDao.totalRowsId3(uid);
    }

    /**
     * 新增数据
     *
     * @param bid 实例对象
     * @return 实例对象
     */
    @Override
    public Bid insert(Bid bid) {
        this.bidDao.insert(bid);
        return bid;
    }

    /**
     * 修改数据
     *
     * @param bid 实例对象
     * @return 实例对象
     */
    @Override
    public Bid update(Bid bid) {
        this.bidDao.update(bid);
        return this.queryById(bid.getBidId());
    }

    /**
     * 通过主键删除数据
     *
     * @param bidId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer bidId) {
        return this.bidDao.deleteById(bidId) > 0;
    }

    /**
     *
     * @param markId
     * @return
     */
    @Override
    public Float Zmoney(int markId) {
        return bidDao.Zmoney(markId);
    }

    /**
     *
     * @param markId
     * @param investor
     * @return
     */
    @Override
    public int count(int markId, int investor) {
        return bidDao.count(markId,investor);
    }

    @Override
    public Float profit(int uid) {
        return bidDao.profit(uid);
    }

    @Override
    public Float interest(int uid) {
        return bidDao.interest(uid);
    }

    @Override
    public void dq(String keyid) {
        //定期
        Bid bid = bidDao.queryById(Integer.valueOf(keyid));
        UserInfo userInfo = userInfoService.userInfoList(bid.getInvestor());
        if(userInfo.getMoney()==null){
            userInfo.setMoney((float) 0);
        }
        userInfo.setMoney((float) (userInfo.getMoney()+bid.getMoney()+bid.getEstimateMoney()));
        bid.setState(2);
        userInfoService.update(userInfo);
        this.update(bid);
    }

    @Override
    public List<Map> sbEacharts(int sid) {
        return bidDao.sbEacharts(sid);
    }
}